package com.华为OD;

import java.util.*;

public class t3射击比赛 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        in.nextLine();
        HashMap<Integer, PriorityQueue<Integer>>hm = new HashMap<>();
        HashMap<Integer,Integer>hms = new HashMap<>();
        String[] ids = in.nextLine().split(",");
        String[] scores = in.nextLine().split(",");
        for (int i = 0; i < N; i++) {
            int id = Integer.parseInt(ids[i]);
            if(!hm.containsKey(id)){
                hm.put(id,new PriorityQueue<>());
            }
            hm.get(id).add(Integer.parseInt(scores[i]));
        }
        hm.entrySet().stream().forEach(entry->{
            int id = entry.getKey();
            PriorityQueue<Integer> pq = entry.getValue();
            if(pq.size()<3)
                return;
            int score = pq.poll()+pq.poll()+pq.poll();
            hms.put(id,score);
        });
        hms.entrySet().stream().sorted((e1, e2) -> {
            if (e1.getValue().equals(e2.getValue()))
                return e2.getKey() - e1.getKey();
            return e2.getValue() - e1.getValue();
        }).forEach(entry->{
            System.out.print(entry.getKey()+" ");
        });
    }
}
